package com.dididoctor.patient.Activity.Login;

import android.content.Context;

import com.dididoctor.patient.Activity.ConstantValue;
import com.dididoctor.patient.Bean.Token;
import com.dididoctor.patient.Http.MyHttpResponseHandler;
import com.dididoctor.patient.MV.BasePresenter;
import com.dididoctor.patient.MV.BusinessClient;
import com.dididoctor.patient.MV.Response;
import com.dididoctor.patient.Utils.SharedPreferencesUtils;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.util.Map;

/**
 * @version 2.0
 * @类描述：
 * @项目名称：ddgh
 * @类名称： LoginPresenter
 * @包名称： com.doudoukeji.service.Activity.Login
 * @创建人： zhanjunjie
 * @创建时间：2016/3/14 15:06
 * @修改人：
 * @修改时间：2016/3/14 15:06
 * @修改备注： new parent class(用一句话描述修改了什么)
 */
public class LoginPresenter extends BasePresenter {

    private LoginView loginView;
    private Context context;

    public LoginPresenter(Context context, LoginView view) {
        super(context, view);
        this.loginView = view;
        this.context = context;
    }

    /**
     * @param phone 获取验证码
     */
    public void getInvitationCode(String phone) {
        RequestParams params = new RequestParams();
        params.put("phone", phone);
        BusinessClient.post(ConstantValue.intCode, params, new MyHttpResponseHandler(loginView) {

            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    loginView.getInvitationCode();
                    //TODO 上线需要去掉
//                    loginView.showToastMessage(response.getJson());
                } else {
                    loginView.getInvitationCodeFail();
                    loginView.showToastMessage(response.getMsg());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                loginView.getInvitationCodeFail();
            }
        });
    }

    public void login(String phone, String checkCode, String inviteCode) {
        RequestParams params = new RequestParams();
        params.put("phone", phone);
        params.put("checkCode", checkCode);
        params.put("inviteCode", inviteCode);
        BusinessClient.post(ConstantValue.LoginUrl, params, new MyHttpResponseHandler(loginView) {
            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    setUserInfoData(response.getData());
                } else {
                    loginView.loginfail(response.getMsg());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                loginView.loginfail("");
            }
        });

    }


    /**
     * 吧
     * 设置登录信息
     */
    private void setUserInfoData(Map<String, Object> userInfo) {

        Token.setId((String) userInfo.get("userId"));
        Token.setPhone((String) userInfo.get("phone"));
        Token.setName((String) userInfo.get("name"));
        Token.setHeadPic((String) userInfo.get("headPic"));
        Token.setSex((String) userInfo.get("sex"));
        Token.setBirth((String) userInfo.get("birth"));
        Token.setIdNo((String) userInfo.get("idNo"));
        Token.setCity((String) userInfo.get("city"));
        Token.setAddress((String) userInfo.get("address"));
        Token.setToken((String) userInfo.get("token"));
        Token.setAccId((String) userInfo.get("accId"));
        Token.setAccPwd((String) userInfo.get("accPwd"));
        String read = SharedPreferencesUtils.getParam(context, "READ", "0");
        if ("1".equals(read)) {
            loginView.intentMainActivity();
        } else {
            loginView.intentDisclaimerActivity();
        }

    }
}
